package cn.com.taurus.controller.datatable;

import cn.com.taurus.bean.DataJson;
import cn.com.taurus.biz.AdminLogBiz;
import cn.com.taurus.biz.AdminsBiz;
import cn.com.taurus.entity.AdminLog;
import cn.com.taurus.entity.Admins;
import cn.com.taurus.util.Log;
import cn.com.taurus.util.UploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.RequestContext;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;

/**
 * @author zjw
 * @createTime 2021年6月28日
 * @remark 后台人员控制层
 * @version 1.0
 */
@Controller
public class AdminsController {
    @Autowired
    private AdminsBiz adminsBiz;

    @RequestMapping("/admins")
    public String powerManagerAndAdmins() {
        return "datatable/admins";
    }

    @RequestMapping("/updateAdmins")
    @Log(operation = "修改后台人员信息",type = 2)
    public String updateSystemNotice(@ModelAttribute Admins admins){
        adminsBiz.adminsUpdate(admins);
        return "datatable/admins";
    }

    /**
     * 模糊查询
     * @param pageNum 几条
     * @param pageSize 数量
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectConditionAdmins")
    @Log(operation = "条件查询后台人员信息",type = 1)
    public HashMap selectConditionAdmins(Integer pageNum, Integer pageSize, Admins admins){
        HashMap<String, Object> map = new HashMap<>();
        // 开启分页支持
        PageHelper.startPage(pageNum,pageSize);
        List<Admins> rule = adminsBiz.selectConditionAdmins(admins);
        PageInfo<Admins> pageInfo = new PageInfo<>(rule);
        System.out.println("数据："+pageInfo.getPages());
        map.put("code",0);
        map.put("msg","请求成功");
        //count：总数据条数
        map.put("count",pageInfo.getTotal());
        //data：数据
        map.put("data",pageInfo.getList());
        return map;
    }

    @RequestMapping("/amendAdmins")
    @Log(operation = "修改个人信息",type = 2)
    public ModelAndView amendAdmins(@ModelAttribute Admins admins,String aAccount){
        adminsBiz.amendAdmins(admins);
        Admins admins2 = adminsBiz.selectAdmisByAAccount(aAccount);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("adminsData",admins2);
        modelAndView.setViewName("major/index");
        return modelAndView;
    }

    /**
     * 分页
     * @param pageNum 第几页
     * @param pageSize 总页面数
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectAdmins")
    public HashMap selectAdmins(Integer pageNum,Integer pageSize){
        //HashMap
        HashMap<String, Object> map = new HashMap<>();
        // 开启分页支持，传入两个参数
        PageHelper.startPage(pageNum,pageSize);
        //查询出数据存入List集合
        List<Admins> rule = adminsBiz.selectAdmins();
        //将集合放入PageInfo构造方法，泛型填写集合的类型
        PageInfo<Admins> pageInfo = new PageInfo<>(rule);
        System.out.println("数据："+pageInfo.getPages());
        //填入layui json格式的数据
        map.put("code",0);
        map.put("msg","请求成功");
        //count：总数据条数
        map.put("count",pageInfo.getTotal());
        //data：数据
        map.put("data",pageInfo.getList()); //pageInfo里面返回的json数据
        System.out.println(map);
        return map;
    }

    @ResponseBody
    @RequestMapping("/importAdmins")
    @Log(operation = "导入后台人员",type = 4)
    public DataJson importAdmins(@RequestParam(value = "file") MultipartFile multipartFile){
        String imagePath = UploadUtil.uploadXLS(multipartFile);
        DataJson dataJson = new DataJson();
        if (imagePath != null){
            dataJson.setCode(1);

            HashMap<String, String> map = new HashMap<>();
            String[] str = imagePath.split("http://localhost:8080/upload");
            String url = str[1];
            map.put("src",url);
            System.out.println("-------------------------------");
            System.out.println("XLS路径为："+url);
            System.out.println("-------------------------------");

            adminsBiz.importAdmins("D://upload/"+url);

            dataJson.setData(map);
        }else {
            dataJson.setCode(0);
        }
        return dataJson;
    }

    /**
     * 批量删除
     * @param deletes 要删的aId数组
     * @return
     */
    @ResponseBody
    @RequestMapping("/deleteAdmins")
    @Log(operation = "批量删除后台人员",type = 3)
    public DataJson deleteAdmins(@RequestParam(value = "deletes[]")Integer[] deletes){
        DataJson dataJson = new DataJson();
        for (Integer integer:deletes) {
            adminsBiz.deleteByAId(integer);
        }
        return dataJson;
    }
}
